package com.mbsnjdxyry.tracord_backend.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mbsnjdxyry.tracord_backend.domain.PassageFollowerNum;
import com.mbsnjdxyry.tracord_backend.domain.PassageInfo;
import com.mbsnjdxyry.tracord_backend.domain.User;
import com.mbsnjdxyry.tracord_backend.domain.UserFollowInfo;
import com.mbsnjdxyry.tracord_backend.domain.vo.PassageToFront;
import com.mbsnjdxyry.tracord_backend.domain.vo.UserToFront;
import com.mbsnjdxyry.tracord_backend.mapper.PassageFollowerNumMapper;
import com.mbsnjdxyry.tracord_backend.mapper.UserFollowInfoMapper;
import com.mbsnjdxyry.tracord_backend.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Component
public class PassageToFrontConverter {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PassageFollowerNumMapper passageFollowerNumMapper;

    @Autowired
    private UserFollowInfoMapper userFollowInfoMapper;

    public List<PassageToFront> getPassageToFrontList(List<PassageInfo> passageList){
        List<PassageToFront> passageToFrontList = new ArrayList<>();

        for (PassageInfo passageInfo : passageList) {
            User user = userMapper.selectById(passageInfo.getUserId());

            PassageToFront passageToFront = new PassageToFront();
            passageToFront.setAvatar(user.getAvatar());
            passageToFront.setNickname(user.getNickName());
            passageToFront.setId(passageInfo.getId());
            passageToFront.setUserId(passageInfo.getUserId());
            passageToFront.setContent((passageInfo.getContent() == null) ? "" : passageInfo.getContent());
            passageToFront.setAddress(passageInfo.getAddress());
            passageToFront.setPhotoTime(passageInfo.getPhotoTime());
            passageToFront.setCreateDate(passageInfo.getCreateDate());
            passageToFront.setImage(passageInfo.getImage());
            passageToFront.setFollowerNum(passageInfo.getFollowerNum());

            LambdaQueryWrapper<PassageFollowerNum> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(PassageFollowerNum::getPassageId,passageInfo.getId());

            List<PassageFollowerNum> passageFollowerNumList = passageFollowerNumMapper.selectList(queryWrapper1);
            if (passageFollowerNumList.isEmpty()){
                passageToFront.setFollowerNum(0);
            }else {
                passageToFront.setFollowerNum(passageFollowerNumList.size());
            }

            //查询当前用户是否点赞了该帖子
            LambdaQueryWrapper<PassageFollowerNum> lqw = new LambdaQueryWrapper<>();
            lqw.eq(PassageFollowerNum::getPassageId,passageInfo.getId())
                    .eq(PassageFollowerNum::getFollowerId,SecurityUtils.getUserId());
            PassageFollowerNum res = passageFollowerNumMapper.selectOne(lqw);
            if(Objects.isNull(res)){
                passageToFront.setIsFollowed(0);
            }else {
                passageToFront.setIsFollowed(1);
            }

            //查询当前用户是否关注了该帖子的博主
            LambdaQueryWrapper<UserFollowInfo> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(UserFollowInfo::getFollowerId,passageInfo.getUserId())
                    .eq(UserFollowInfo::getUserId,SecurityUtils.getUserId());
            Integer res2 = userFollowInfoMapper.selectCount(lqw2);
            if(res2 > 0){
                passageToFront.setIsUserFollowed(1);
            }else {
                passageToFront.setIsUserFollowed(0);
            }

            List<UserToFront> userToFrontList = new ArrayList<>();
            for (int i = 0; i < passageFollowerNumList.size(); i++) {
                if (i == 3) {
                    break;
                }
                LambdaQueryWrapper<User> queryWrapper2 = new LambdaQueryWrapper<>();
                queryWrapper2.eq(User::getId, passageFollowerNumList.get(i).getFollowerId());
                User user1 = userMapper.selectOne(queryWrapper2);
                userToFrontList.add(new UserToFront(user1.getId(),user1.getAvatar()));
            }

            passageToFront.setUserList(userToFrontList);
            passageToFrontList.add(passageToFront);
        }

        return passageToFrontList;
    }
}
